//
//  WelcomeViewController.swift
//  Tanenbaum_blog
//
//  Created by Tanenbaum on 15/7/31.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {
    
    private var iconBottomCons: NSLayoutConstraint?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        prepareUI()
        
        
        //加载用户头像
       if let urlString = UserAccount.shareAccount?.avatar_large {
            
            iconImageView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
    }

    override func viewDidAppear(animated: Bool) {
        
         super.viewDidAppear(animated)
        //
        iconBottomCons?.constant = -UIScreen.mainScreen().bounds.height-iconBottomCons!.constant
        
        //动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            }) { (_ ) -> Void in
              
                NSNotificationCenter.defaultCenter().postNotificationName(ZuoJRootViewControllerSwitchNotification, object: true)
        }
        
    }
    private func prepareUI() {
        
        // 添加子控件视图
         view.addSubview(backImageView)
         view.addSubview(iconImageView)
         view.addSubview(label)
        
        //添加约束
        //背景
        backImageView.ff_Fill(view)
        
        //头像
        let cons = iconImageView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        
        //获取底部约束
        iconBottomCons = iconImageView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        //欢迎标签
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconImageView, size: nil, offset: CGPoint(x: 0, y: 16))
        
    }
    
    //MARK: --懒加载控件
    //加载开始背景图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    private lazy var iconImageView: UIImageView = {
       
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
      
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        return iv
    }()
    
    //欢迎标签
    private lazy var label: UILabel = {
       
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        
        return label
        
    }()
}
